<template>
  <!-- loading 进度条的显示 -->
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools 
          :item="company" 
          :isNodeShow="true" 
          @addDep="addDep" 
          @delSuccess="getDepartments" 
        />

        <!-- el-tree 树形控件 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 departs有多少数据 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象 node 每个node对象-->
        <tree-tools 
          slot-scope="{ data:item }" 
          :item="item" 
          @addDep="addDep"
          @updDep="updDep" 
          @delSuccess="getDepartments"
        />
        </el-tree>
      </el-card>
    </div>
      <!-- 新增部门 -->
      <add-dept
        ref="addDept" 
        :item="node" 
        :isAddDeptShow.sync="isAddDeptShow"
        @addDep="getDepartments"
      />
  </div>
</template>

<script>
import treeTools from './components/tree-tools'
import addDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'departments',
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      isAddDeptShow: false, // 设置新增部门弹框的状态
      node: null, // 数组对象
      loading: false // 用来控制进度弹层的显示和隐藏
    }
  },
  created() {
    this.getDepartments() // 调用获取数据的接口
   },
  methods: {
    async getDepartments() {
      try {
        this.loading = true
        const res = await getDepartments()
        // console.log(res);
        // this.company = { name: res.companyName, manager: '负责人', id: '' }
        this.company = { name: '深圳市腾讯计算机系统有限公司', manager: '负责人', id: '' }
        
        this.departs = tranListToTreeData(res.depts, '')
        this.loading = false
      } catch (error) {
        console.log(error);
      }
    },
    addDep(node) {
      this.isAddDeptShow = true
      this.node = node
    },
    updDep(node) {
      this.isAddDeptShow = true // 打开编程页面
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
    }
  },
  components: {
    treeTools,
    addDept
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
